package com.encryption.dao;

import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.encryption.model.File;
import com.encryption.model.FilePermissionAsso;
import com.encryption.model.User;

/**
 * A data access object (DAO) providing persistence and search support for
 * FilePermissionAsso entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.encryption.model.FilePermissionAsso
 * @author MyEclipse Persistence Tools
 */

public class FilePermissionAssoDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory
			.getLogger(FilePermissionAssoDAO.class);
	// property constants
	public static final String ALLOW_TYPE = "allowType";
	public static final String ALLOW_VALUE = "allowValue";
	public static final String ALLOW_VIEW = "allowView";
	public static final String ALLOW_DOWNLOAD = "allowDownload";
	public static final String ALLOW_PRINT = "allowPrint";

	public void save(FilePermissionAsso transientInstance) {
		log.debug("saving FilePermissionAsso instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(FilePermissionAsso persistentInstance) {
		log.debug("deleting FilePermissionAsso instance");
		try {
			getSession().delete(persistentInstance);
			getSession().flush();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public FilePermissionAsso findById(java.lang.Integer id) {
		log.debug("getting FilePermissionAsso instance with id: " + id);
		try {
			FilePermissionAsso instance = (FilePermissionAsso) getSession()
					.get("com.encryption.model.FilePermissionAsso", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(FilePermissionAsso instance) {
		log.debug("finding FilePermissionAsso instance by example");
		try {
			List results = getSession()
					.createCriteria("com.encryption.model.FilePermissionAsso")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding FilePermissionAsso instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from FilePermissionAsso as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByAllowType(Object allowType) {
		return findByProperty(ALLOW_TYPE, allowType);
	}

	public List findByAllowValue(Object allowValue) {
		return findByProperty(ALLOW_VALUE, allowValue);
	}

	public List findByAllowView(Object allowView) {
		return findByProperty(ALLOW_VIEW, allowView);
	}

	public List findByAllowDownload(Object allowDownload) {
		return findByProperty(ALLOW_DOWNLOAD, allowDownload);
	}

	public List findByAllowPrint(Object allowPrint) {
		return findByProperty(ALLOW_PRINT, allowPrint);
	}

	public List findAll() {
		log.debug("finding all FilePermissionAsso instances");
		try {
			String queryString = "from FilePermissionAsso";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public FilePermissionAsso merge(FilePermissionAsso detachedInstance) {
		log.debug("merging FilePermissionAsso instance");
		try {
			FilePermissionAsso result = (FilePermissionAsso) getSession()
					.merge(detachedInstance);
			log.debug("merge successful");
			getSession().flush();
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(FilePermissionAsso instance) {
		log.debug("attaching dirty FilePermissionAsso instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(FilePermissionAsso instance) {
		log.debug("attaching clean FilePermissionAsso instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	/**
	 * 通过用户或部门id和文件id,
	 * @param propertyName
	 * @param value
	 * @return
	 */
	public List<FilePermissionAsso> hasPermission(Integer fileId,Integer userOrDepartmentId,Integer allowType) {
		try {
			String queryString = "from FilePermissionAsso as model where model.file"
					+ fileId + "= ? and model.allowValue = ? and model.allowType = ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, fileId);
			queryObject.setParameter(1,userOrDepartmentId);
			queryObject.setParameter(1,allowType);
			
			return (List<FilePermissionAsso>)queryObject.list();
		} catch (RuntimeException re) {
			log.error("hasPermission failed", re);
			throw re;
		}
	}
	
	public List<FilePermissionAsso> getPermissionsByCondition(List<File> fileList){
		try {
			String fileStr = "";
			for (File file : fileList){
				int fileId = file.getId(); 
				fileStr += fileId + ",";
			}
			fileStr = fileStr.substring(0, fileStr.length() - 1);
			
			String queryString = "select * from file_permission_asso where file_id in ("+ fileStr+")";
			Query queryObject = getSession().createSQLQuery(queryString).addEntity(FilePermissionAsso.class);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public void deleteByFileId(int fileId){
		log.debug("deleting by fileId");
		try {
			String deleteSql = "delete from file_permission_asso where file_id=" + fileId;
			SQLQuery query = getSession().createSQLQuery(deleteSql);
			query.executeUpdate();
			log.debug("delete fileId successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	private static FilePermissionAssoDAO filePermissionAssoDAO = null;
	
	
	public static  FilePermissionAssoDAO getFilePermissionAssoDAO(){
		if(null == filePermissionAssoDAO) {
			filePermissionAssoDAO = new FilePermissionAssoDAO();
		} 
		return filePermissionAssoDAO;
	}
	
}